// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    // API 调用都保持和云函数当前所在环境一致
    env: cloud.DYNAMIC_CURRENT_ENV,
})

// 云函数入口函数
exports.main = async (event, context) => {
    const db = cloud.database();

    const {activityId, userId} = event;

    // 更新 Activity
    await db.collection('activity').where({
        _id: activityId,
        'joiners.id': userId
    }).update({
        data: {
            'joiners.$.quit': true,
            'joiners.$.score': -50,
        }
    });

    // 更新 User
    await db.collection('user').where({
        _id: userId,
        'activities.id': activityId
    }).update({
        data: {
            'activities.$.quit': true,
            'activities.$.score': -50,
        }
    });

    return {
        status: 0
    };
}
